﻿using AutoMapper;
using Furion;
using Furion.DependencyInjection;
using Furion.LinqBuilder;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;
using XinGuanERP.Db;
using XinGuanERP.Models.Po.system;
using XinGuanERP.Models.Vo.system;
using XinGuanERP.Services.system.IServices;

namespace XinGuanERP.Services.system
{
    public class DeptService : IDeptService, ITransient
    {
        private readonly DefaultDbContext _defaultDbContext;
        public DeptService(DefaultDbContext defaultDbContext)
        {
            _defaultDbContext = defaultDbContext;
        }
        public bool DeleteDept(string Id)
        {
            var res = _defaultDbContext.Depts.Where(t => t.Id == Id).FirstOrDefault();
            if (res != null)
            {
                _defaultDbContext.Depts.Remove(res);
                return _defaultDbContext.SaveChanges() > 0;
            }
            else
            {
                return false;
            }
        }

        public List<DeptVo> GetAllDepts(string? deptName, string? status)
        {
            var res = new List<Dept>();
            if (!deptName.IsNullOrEmpty() && status.IsNullOrEmpty())
            {
                res = _defaultDbContext.Depts
                    .Where(t => EF.Functions.Like(t.DeptName, "%" + deptName + "%"))
                    .OrderBy(t => t.OrderNo).ToList();
            }

            if (!status.IsNullOrEmpty() && deptName.IsNullOrEmpty())
            {
                res = _defaultDbContext.Depts
                    .Where(t => EF.Functions.Like(t.Status, "%" + status + "%"))
                    .OrderBy(t => t.OrderNo).ToList();
            }

            if (!deptName.IsNullOrEmpty() && !status.IsNullOrEmpty())
            {
                res = _defaultDbContext.Depts
                    .Where(t => EF.Functions.Like(t.DeptName, "%" + deptName + "%"))
                    .Where(t => EF.Functions.Like(t.Status, "%" + status + "%"))
                    .OrderBy(t => t.OrderNo).ToList();
            }

            if (deptName.IsNullOrEmpty() && status.IsNullOrEmpty())
            {
                res = _defaultDbContext.Depts.OrderBy(t => t.OrderNo).ToList();
            }


            List<DeptVo> viewDepts = new();
            foreach (var Dept in res)
            {
                // 不是一级菜单
                if (Dept.ParentDept != null)
                {
                    continue;
                }
                DeptVo viewDept =
                    new()
                    {
                        Id = Dept.Id,
                        DeptName = Dept.DeptName,
                        OrderNo = Dept.OrderNo,
                        Status = Dept.Status,
                        ParentDept = Dept.ParentDept,
                        CreateTime = Dept.CreateTime,
                        Remark = Dept.Remark,
                        Children = BuildDeptTree(Dept, res)
                    };
                viewDepts.Add(viewDept);
            }

            return viewDepts;
        }

        public bool UpdateDept(DeptVo viewDept)
        {
            var res = _defaultDbContext.Depts.FirstOrDefault(x => x.Id == viewDept.Id);
            var config = new MapperConfiguration(cfg => cfg.CreateMap<DeptVo, Dept>()
                .BeforeMap((src, des) => src.Id = Guid.NewGuid().ToString().Replace("-", "").ToUpper())
                .BeforeMap((src, des) => des.CreateTime = DateTime.Now)
                .BeforeMap((src, des) => des.CreateUserId = App.User?.FindFirstValue("UserId"))
                .BeforeMap((src, des) => des.CreateUserName = App.User?.FindFirstValue("UserName")));
            var mapper = config.CreateMapper();
            if (res == null)
            {
                _defaultDbContext.Depts.Add(
                    mapper.Map<Dept>(viewDept)
                );
            }
            else
            {
                res = new MapperConfiguration(cfg => cfg.CreateMap<DeptVo, Dept>()
                    .BeforeMap((src, des) => des.UpdateTime = DateTime.Now)
                    .BeforeMap((src, des) => des.UpdateUserId = App.User?.FindFirstValue("UserId"))
                    .BeforeMap((src, des) => des.UpdateUserName = App.User?.FindFirstValue("UserName")))
                    .CreateMapper().Map(viewDept, res);
            }

            return _defaultDbContext.SaveChanges() > 0;
        }

        public List<DeptVo> BuildDeptTree(Dept dept, List<Dept> depts)
        {
            List<DeptVo> children = new();
            //遍历所有目录
            foreach (var Dept in depts)
            {
                if (Dept.ParentDept != null && Dept.ParentDept.Equals(dept.Id))
                {
                    DeptVo viewDept = new()
                    {
                        Id = Dept.Id,
                        DeptName = Dept.DeptName,
                        OrderNo = Dept.OrderNo,
                        Status = Dept.Status,
                        ParentDept = Dept.ParentDept,
                        CreateTime = Dept.CreateTime,
                        Remark = Dept.Remark,
                        Children = BuildDeptTree(Dept, depts)
                    };
                    children.Add(viewDept);
                }
            }
            if (children.Count == 0)
            {
                return null;
            }
            return children;
        }
    }
}
